<?php
    /**
    * Get User activity chart
    * For Pray Online
    * 
    * Note: Permission set on openidmodule, because, loginmodule does not have permission
    */

    if (!defined('EXPONENT')) exit('');
    $chart_length=30; // 30 ngày
    $chart_line_color=array("#5B56B6", "#DFC329", "#6363AC", "#5E472", "#000000", "#e4f0db", "#d48bc6", "#97571c", "#999999");

    // check permission first
    $loc=null;
    $loc->mod="openidmodule"; // because, loginmodule does not have permission, so we set permission on openidmodule
    if (exponent_permissions_check("edit_userprofile",$loc) ||
    exponent_permissions_check("administrate",$loc))
    {
        $id=-1;
        if (isset($_REQUEST['id']))
        {
            $id=intval($_REQUEST['id']);

            $cuser=exponent_users_getUserById($id);

            $html="";
            // today
            $time=aboid_getCurrentTime();
            $day=date("d",$time);
            $month=date("m",$time);
            $year=date("Y",$time);
            // 30 days before
            $temp=mktime(23,59,59,$month, $day, $year);
            $time_now=$temp;
            $temp=strtotime("-{$chart_length} days", $temp);
            $yday=date("d",$temp);
            $ymonth=date("m",$temp);
            $yyear=date("Y",$temp);
            $time_before=mktime(0,0,0,$ymonth, $yday, $yyear);

            // get all rings
            $rings=$db->selectObjects("pray_ring");
            // get data
            $sql="SELECT {$db->prefix}demand_praydetail.*, {$db->prefix}demands.ring_id ring_id 
            FROM {$db->prefix}demand_praydetail 
            INNER JOIN {$db->prefix}demands
            ON {$db->prefix}demands.id = {$db->prefix}demand_praydetail.demand_id
            WHERE (add_time >= {$time_before}) AND (add_time <= {$time_now}) AND ({$db->prefix}demand_praydetail.user_id = {$cuser->id})
            ORDER BY vyear, vmonth, vday
            ";
            $query_data=$db->selectObjectsBySql($sql);

            include BASE.'modules/loginmodule/actions/php5_ofc_lib/open-flash-chart.php';

            $title = new title("Biểu đồ hoạt động của {$cuser->username} từ {$yday}/{$ymonth}/{$yyear} đến {$day}/{$month}/{$year}");
            $title->set_style( "{font-size: 14px; font-family: Times New Roman; font-weight: bold; color: #A2ACBA; text-align: center;}" );

            $chart_y_min=0;
            $chart_y_max=0;

            // query data
            $data=array(); // contains array of array
            $data_label=array();

            $temp_day=$yday;
            $temp_month=$ymonth;
            $temp_year=$yyear;
            $temp_current_time=$time_before;
            $temp_index=1;

            foreach ($rings as $k=>$v){
                $data[$v->id]=array();
                for ($i=0;$i<$chart_length;$i++)
                    $data[$v->id][$i]=0;
            }

            do{
                $data_label[]="{$temp_day}/{$temp_month}";
                //
                foreach ($query_data as $k=>$qdata)
                    if (($qdata->vday == $temp_day) 
                    && ($qdata->vmonth == $temp_month) 
                    && ($qdata->vyear == $temp_year ))
                    {
                        $data[$qdata->ring_id][$temp_index]++;
                        if ($data[$qdata->ring_id][$temp_index] > $chart_y_max)
                        {
                            $chart_y_max=$data[$qdata->ring_id][$temp_index];
                        }
                        unset($query_data[$k]);
                    }
                    else
                    {
                        break; // because, this array is sorted before, so, we can break to prevent long time genrate
                }
                // if current day, this user only click less than 3 times
                if (($data[$qdata->ring_id][$temp_index]>0) &&
                    ($data[$qdata->ring_id][$temp_index] < 4))
                {
                    $s = new star($data[$qdata->ring_id][$temp_index]);
                    $data[$qdata->ring_id][$temp_index] = $s->colour('#ff0000');
                }
                // calculate new date value
                $temp_index++;
                $temp_current_time=strtotime("+1 day",$temp_current_time);
                $temp_day=date("d",$temp_current_time);
                $temp_month=date("m",$temp_current_time);
                $temp_year=date("Y",$temp_current_time);

            }while ($temp_current_time < $time_now);

            // new chart object
            $chart = new open_flash_chart();
            // set title
            $chart->set_title( $title );

            $x_labels = new x_axis_labels();
            $x_labels->set_steps( 1 );
            $x_labels->set_vertical();
            $x_labels->set_colour( '#000000' );
            $x_labels->set_labels( $data_label );

            $x = new x_axis();
            $x->set_colour( '#A2ACBA' );
            $x->set_grid_colour( '#D7E4A3' );
            $x->set_offset( false );
            $x->set_steps(4);
            // Add the X Axis Labels to the X Axis
            $x->set_labels( $x_labels );

            $chart->set_x_axis( $x );


            //  build data for each ring
            foreach ($data as $k=>$v)
            {
                // search ring name
                $ring_name="Unknown";
                foreach ($rings as $ringk=>$ringv)
                    if ($ringv->id == $k)
                    {
                        $ring_name=$ringv->name;
                        break;
                    }
                    $area = new line();
                    //$area->set_tooltip("Ngày , xác nhận đã cầu nguyện #val# lần");
                    $area->set_width( 2 );
                    $area->set_colour( array_pop($chart_line_color));
                    $area->set_values( $v );
                    $area->set_key($ring_name , 11 );
                //
                $chart->add_element( clone $area);
            }

            //
            // remove this when the Y Axis is smarter
            //
            $y = new y_axis();
            $y->set_range( $chart_y_min, $chart_y_max+3, floor($chart_y_max/5));
            $chart->add_y_axis( $y );

            $html=$chart->toString();

            $data=null;
            $data->id=$id;
            $data->chart_content=$html;
            echo json_encode($data);
        }
        else
        {
            echo "";
        }
    }
    else
    {
        echo "";
    }
?>
